//
//  HLSettingViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/19.
//

#import "HLSettingViewController.h"
#import "HLMineCell.h"
#import "HLSettingAccountViewController.h"
#import "HLInviteViewController.h"
#import "HLAboutViewController.h"
#import "HLFeedbackViewController.h"
#import "HLBlackListViewController.h"
#import "HLUpdateTipView.h"

// 53客服
#import <CSVisitorSDK/CSVisitorChatViewController.h>
#import <CSVisitorSDK/CSVisitorSDK.h>
#import <CSVisitorSDK/CSCustomInfoModel.h>

@interface HLSettingViewController ()<CS53ServiceDelegate>

@end

@implementation HLSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HLBackgroundColor;
    
    [[CS53Manager sharedManager] login53ServiceWithVisitorId:[HLRunTime shareInstance].mine.userYueId];
    [CS53Manager sharedManager].delegate = self;
}

- (void)setupNavigationItems
{
    [super setupNavigationItems];
    self.title = @"设置";
}

- (void)initTableView
{
    [super initTableView];
    
    [self.datasources addObjectsFromArray:[HLMineCellModel cellWithSettings]];
    
    self.tableView.backgroundColor = HLBackgroundColor;
    [self.tableView registerClass:[HLMineCell class] forCellReuseIdentifier:[HLMineCell identifyCell]];
    
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDarkContent;
}

- (UIImage *)qmui_navigationBarBackgroundImage
{
    return [UIImage qmui_imageWithColor:UIColorClear];
}

- (nullable UIColor *)qmui_navigationBarTintColor
{
    return UIColorBlack;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datasources.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *sections = self.datasources[section];
    return sections.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HLMineCell *cell = [tableView dequeueReusableCellWithIdentifier:[HLMineCell identifyCell]];
    cell.model = self.datasources[indexPath.section][indexPath.row];
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = HLBackgroundColor;
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 12;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLMineCellModel *model = self.datasources[indexPath.section][indexPath.row];
    
    switch (model.cellSpot) {
        case HLMineCellSpotSettingAccount:
        {
            HLSettingAccountViewController *settingAccountVC = [[HLSettingAccountViewController alloc] init];
            [self.navigationController pushViewController:settingAccountVC animated:YES];
        }   break;
        case HLMineCellSpotSettingInvitation:
        {
            HLInviteViewController *inviteVC = [[HLInviteViewController alloc] init];
            [self.navigationController pushViewController:inviteVC animated:YES];
        }   break;
        case HLMineCellSpotSettingAbout:
        {
            HLAboutViewController *aboutVC = [[HLAboutViewController alloc] init];
            [self.navigationController pushViewController:aboutVC animated:YES];
        }   break;
        case HLMineCellSpotSettingFeedback:
        {
            HLFeedbackViewController *feedbackVC = [[HLFeedbackViewController alloc] init];
            [self.navigationController pushViewController:feedbackVC animated:YES];
        }   break;
        case HLMineCellSpotSettingPrivacyBlack:
        {
            HLBlackListViewController *blackVC = [[HLBlackListViewController alloc] init];
            [self.navigationController pushViewController:blackVC animated:YES];
        }   break;
        case HLMineCellSpotSettingCustomer:
        {
            [self contactUS];
        }   break;
        case HLMineCellSpotSettingUpdate:
        {
            [HLUpdateTipView requestUpdateWithBlock:^{
                [QMUITips showWithText:@"当前已是最新版本"];
            } isCoerce:NO];
        }   break;
        case HLMineCellSpotSettingLogout:
        {
            [HLHelper shanYanLoadWithCallback:^{}]; //退出时重新获取shanYan数据
            [HLUIHelper showAlertWithContent:@"是否退出该帐号？" callBack:^(BOOL isConfirm) {
                if (isConfirm){
                    [HLHelper logout];
                    if ([HLRunTime shareInstance].isFood){
                        [HLUIHelper setRootTabBar];
                    } else {
                        [HLUIHelper setRootLogin];
                    }
                }
            }];
        }   break;
            
        default:
            break;
    }
}



- (void)contactUS {
    
    CSVisitorChatViewController *chatVC = [[CSVisitorChatViewController alloc] initWithArg:CS53_ARG style:@"1"];
    chatVC.title = @"客服";
    [self.navigationController pushViewController:chatVC animated:YES];
}


#pragma mark - CS53ServiceDelegate

- (void)didFinishLoad{
    [[CS53Manager sharedManager] loadChatList];
    CSCustomInfoModel *model = [[CSCustomInfoModel alloc] init];
    model.visitorId = [HLRunTime shareInstance].mine.userYueId;
    model.arg = CS53_ARG;
    model.userId = @"";
    model.username = [HLRunTime shareInstance].mine.nickName;
    model.phone = [HLRunTime shareInstance].mine.mobileNo;
    [[CS53Manager sharedManager] registerCustomInfo:model];
}

@end
